package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Emp;
import com.woniuxy.woniuhis.exception.EmpServiceException;
import com.woniuxy.woniuhis.service.EmpService;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
public class EmpController {

	@Autowired
	EmpService empService;

	/**
	 * 添加员工信息
	 * @param emp
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addEmp")
	public ResponseInfo addEmp(Emp emp) {
		System.out.println("添加员工:"+emp);
		// 设置初始密码
		emp.setEmpPassword("123456");
		try {
			empService.addEmp(emp);
			ResponseInfo respInfo = new ResponseInfo(200, "添加成功");
			return respInfo;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			ResponseInfo respInfo = new ResponseInfo(200, "添加失败");
			return respInfo;
		}
	}

	/**
	 * 	查询所有员工信息
	 * @param emp
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findEmps")
	public ResponseJson<Emp> findEmps(Emp emp,
			@RequestParam(value = "page", defaultValue = "1", required = false) Integer pageIndex,
			@RequestParam(value = "limit", defaultValue = "5", required = false) Integer pageSize) {
		ResponseJson<Emp> respjson = new ResponseJson<Emp>();
		System.out.println(emp);
		try {
			PageInfo<Emp> page = empService.findEmps(emp, pageIndex, pageSize);
			respjson.setData(page.getList());
			respjson.setCount((int) page.getTotal());
			respjson.setCode(200);
			return respjson;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			respjson.setCode(500);
			respjson.setMsg("没有查到数据");
			return respjson;
		}
	}

	/**
	 * 按主键查询员工 员工详情页
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findEmpdetails")
	public ModelAndView findEmpdetails(HttpServletRequest req) {
		String empId = req.getParameter("empId");
		try {
			System.out.println(empId);
			Emp emp = empService.findEmpByempId(empId);
			ModelAndView mv = new ModelAndView();

			mv.addObject("emp", emp);
			mv.setViewName("views/system/empInfoDetail.html");
			return mv;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			ModelAndView mv = new ModelAndView();
			mv.addObject("msg", "加载员工信息失败");
			mv.setViewName("msg.html");
			return mv;
		}
	}

	/**
	 * 根据主键查找员工信息 在更新
	 * 
	 * @param req
	 * @return
	 */
	String browserempId;

	@ResponseBody
	@RequestMapping(value = "/findEmptoUpdate")
	public ModelAndView findEmptoUpdate(HttpServletRequest req) {
		browserempId = req.getParameter("empId");
		try {
			System.out.println(browserempId);
			Emp emp = empService.findEmpByempId(browserempId);
			System.out.println(emp);
			ModelAndView mv = new ModelAndView();
			mv.addObject("emp", emp);
			mv.setViewName("views/system/updateEmpInfo.html");
			return mv;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			ModelAndView mv = new ModelAndView();
			mv.addObject("msg", "跳转失败");
			mv.setViewName("msg.html");
			return mv;
		}
	}

	/**
	 * 更新员工信息
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateEmpInfo")
	public ResponseInfo upEmpInfo(HttpServletRequest req) {
		Emp emp = new Emp();
		if (browserempId != null) {
			emp.setEmpId(browserempId);
		}
		emp.setEmpName(req.getParameter("empName"));
		emp.setEmpPassword(req.getParameter("empPassword"));
		emp.setEmpGender(req.getParameter("empGender"));
		emp.setEmpTel(req.getParameter("empTel"));
		emp.setEmpIdcard(req.getParameter("empIdcard"));
		emp.setHospitalId(req.getParameter("hospitalId"));
		emp.setOfficeId(req.getParameter("officeId"));
		emp.setDepId(req.getParameter("depId"));
		// 日期格式转换
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// 注意月份是MM
//		String hiredateSource = req.getParameter("empHiredate");
//		System.out.println(hiredateSource);
//		Date hiredate;
		try {
//			hiredate = simpleDateFormat.parse(hiredateSource);
//			emp.setEmpHiredate(hiredate);
			System.out.println(emp);
			empService.modifyEmpByEmpId(emp);
			ResponseInfo respinfo = new ResponseInfo();
			respinfo.setMsg("修改成功");
			return respinfo;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			empService.modifyEmpByEmpId(emp);
			ResponseInfo respinfo = new ResponseInfo();
			respinfo.setMsg("修改失败");
			return respinfo;
		}
	}

	/**
	 * 根据主键删除指定员工
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delEmpInfo")
	public ResponseJson<Emp> delEmpInfo(String empId) {
		ResponseJson<Emp> respJson = new ResponseJson<Emp>();
		if(StringUtils.isEmpty(empId)) {
			respJson.setCode(500);
			respJson.setMsg("员工编号为空");
			return respJson;
		}
		try {
			System.out.println(empId);
			empService.delEmpByempId(empId);
			respJson.setCode(200);
			respJson.setMsg("删除成功");
			return respJson;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			respJson.setCode(500);
			respJson.setMsg("删除失败");
			return respJson;
		}
	}

	/**
	 * 按条件员工信息 没有实现
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findEmpCondition")
	public ResponseJson<Emp> findEmpCondition(HttpServletRequest req) {
		Emp emp = new Emp();
		emp.setEmpName(req.getParameter("empName"));
		emp.setEmpTel(req.getParameter("empTel"));
		emp.setEmpIdcard(req.getParameter("empIdcard"));
		emp.setHospitalId(req.getParameter("hospitalId"));
		Integer pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
		Integer pagesize = Integer.parseInt(req.getParameter("pageSize"));

		try {

			List<Emp> findemps = empService.findEmpByCondition(emp, pageIndex, pagesize);
			ResponseJson<Emp> respJson = new ResponseJson<Emp>();
			respJson.setCode(200);
			respJson.setData(findemps);
			return respJson;
		} catch (EmpServiceException e) {
			e.printStackTrace();
			ResponseJson<Emp> respJson = new ResponseJson<Emp>();
			respJson.setCode(200);
			respJson.setMsg("没有找到数据");
			return respJson;

		}

	}

}
